<?php
/**
 * Created by XM·Tan.
 * Motto: Who shuts love out, in turn shall be shut out from love.
 * Date: 2021/3/23 2:29
 */

namespace app\common\lib;


class ArrayLib {

    /**
     * 权限数据结构重组
     * @param $result
     * @return array
     */
    public static function setAuthArray($result) {
        $list = array();
        foreach ($result as $k => $v) {
            $list[$k]['id'] = $v['id'];
            $list[$k]['name'] = $v['title'];
            $list[$k]['open'] = true;
            $list[$k]['checked'] = false;
            $list[$k]['pid'] = $v['pid'];
            $list[$k]['value'] = $v['id'];
        }
        return $list;
    }

    /**
     * 判断是否已被选中
     * @param $result 二维数组
     * @param $checked 一维数组
     * @param $str
     * @return mixed
     */
    public static function setAuthChecked($result, $checked, $str = 'checked') {
        $checkedArray = explode(',', $checked);
        foreach ($result as $k => $v) {
            foreach ($checkedArray as $cv) {
                if ($v['id'] == $cv) {
                    $result[$k][$str] = true;
                }
            }
        }
        return $result;
    }

    /**
     * 获取所有角色中的权限，并拼接
     * @param $data
     * @return array|string
     */
    public static function getPushStringToArray($data) {
        if (empty($data)) {
            return [];
        }
        $str = '';
        foreach ($data as $k => $v) {
            $str = $str . $v['rules'] . ',';
        }
        $array = explode(',', $str);
        return array_unique(array_filter($array));
    }

    /**
     * 分类树， 支持无限极分类
     * @param $data
     * @param string $child
     * @return array
     */
    public static function getTree($data, $child = 'children') {
        $items = [];
        foreach ($data as $v) {
            $items[$v['id']] = $v;
        }
        $tree = [];
        foreach ($items as $id => $item) {
            if (isset($items[$item['pid']])) {
                $items[$item['pid']][$child][] = &$items[$id];
            } else {
                $tree[] = &$items[$id];
            }
        }
        return $tree;
    }

    /**
     * 在数组指定位置插入数组
     * @param array $array	原数组
     * @param int $position	需要插入到原数组的位置
     * @param array $insert_array	需要插入的数组
     * @return array
     */
    public static function designatPosInsertArray(&$array, $position, $insert_array){
        $first_array = array_splice($array, 0, $position);
        $array = array_merge($first_array, $insert_array, $array);
        return $array;
    }

    /**
     * 分页默认返回数据
     * @param $limit
     * @return array
     */
    public static function getPaginateDefaultData($limit) {
        $result = [
            'total' => 0,
            'per_page' => $limit,
            'current_page' => 1,
            'last_page' => 0,
            'data' => [],
        ];
        return $result;
    }

    /**
     * 二维数组排序
     * @param $result
     * @param $key
     * @param int $sort
     * @return array
     */
    public static function arrsSortByKey($result, $key, $sort = SORT_DESC) {
        if(!is_array($result) || !$key) {
            return [];
        }
        array_multisort(array_column($result, $key), $sort, $result);
        return $result;
    }
}